<?php
class template{
    //var $name = '';
	
	public function template ( $args ) {
		$this->config = $args;
	}
	
    public function begin ( $parent = 0 ) {
        global $db;
        $result = $db->sql_query("SELECT core_template_unit_types.id,
                                         core_template_unit_types.name,
                                         core_template_unit_types.split,
                                         core_template_units.tag_attrs,
                                         core_template_units.tag_content
                                  FROM core_template_unit_types JOIN core_template_units
                                  ON core_template_units.unit_types_id=core_template_unit_types.id
                                  WHERE core_template_units.act=1 AND
                                         core_template_units.templ_id=(SELECT id FROM core_template_name WHERE name='".$this->config['name']."')
                                  AND core_template_unit_types.parent='".$parent."'
                                  ORDER BY core_template_unit_types.id ASC");
        while(list($id,$tag,$split,$attrs,$content) = $db->sql_fetchrow($result)){
            echo '<'.$tag.( $attrs != '' ? ' '.$attrs:'').'>' . $this->eval_content ( $content );
            $childcount = $this->begin($id);
            echo ($split == 1 || ($childcount == 0 && $parent == 0)) ? '':('</'.$tag.'>');
        }
        return $db->sql_numrows($result);
    }


    public function cout ( $parent = 0 ) {
        global $db;
        $result = $db->sql_query("SELECT core_template_unit_types.id,
                                      core_template_unit_types.name
                                  FROM core_template_unit_types JOIN core_template_units
                                  ON core_template_units.unit_types_id=core_template_unit_types.id
                                  WHERE core_template_units.act=1 AND
                                         core_template_units.templ_id=(SELECT id FROM core_template_name WHERE name='".$this->config['name']."')
                                  AND core_template_unit_types.parent='".$parent."'
                                  AND core_template_unit_types.split = 1
                                  ORDER BY core_template_unit_types.id DESC");
        while(list($id,$tag) = $db->sql_fetchrow($result)){
            echo '</'.$tag.'>';
        }
    }
	
	public function img_path ( $mdl ) {
		return 'cside/images/'.$this->config['name'].'/'.$mdl.'';
	}
	
	private function eval_content ( $content ) {
		global $forms;
		$form = "\$r_file=\"" . $forms->cor_slashes( addslashes ( $content ) ) . "\";";
		eval ( $form );
		return stripslashes( $r_file );
	}
}
?>
